package ua.edu.test.tab1actions.unique.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import ua.edu.test.AtributesBean;
import ua.edu.test.R;
import ua.edu.test.db.Card;
import ua.edu.test.db.DBWork;
import ua.edu.test.tab1actions.unique.dialogsfragments.MoneyTransferDialog;

import java.util.List;

/**
 * Created by Max on 16.07.2014.
 */
public class MoneyTransfer extends Fragment {

	private static final String LOG_TAG = MoneyTransfer.class.getName();

	private EditText editTransferCardNumberMT; // 16-и значный номер карты, на которую пользователь переводит деньги
	private EditText editTransferSumMT; // сумма для перевода
	private CheckBox cbUan;
	private CheckBox cbUsd;
	private CheckBox cbEur;
	private CheckBox cbRus;
	private String transferCurrency; // Валюта перевода
	private Button btnMoneyTransferMT; //выполнить перевод

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_tab1_money_transfer, null);

		editTransferCardNumberMT = (EditText)view.findViewById(R.id.editTransferCardNumberMT);
		editTransferSumMT = (EditText)view.findViewById(R.id.editTransferSumMT);

		cbUan = (CheckBox)view.findViewById(R.id.cbUan);
		cbUsd = (CheckBox)view.findViewById(R.id.cbUsd);
		cbEur = (CheckBox)view.findViewById(R.id.cbEur);
		cbRus = (CheckBox)view.findViewById(R.id.cbRus);

		cbUan.setOnCheckedChangeListener(listener);
		cbUsd.setOnCheckedChangeListener(listener);
		cbEur.setOnCheckedChangeListener(listener);
		cbRus.setOnCheckedChangeListener(listener);

		btnMoneyTransferMT = (Button)view.findViewById(R.id.btnMoneyTransferMT);
		btnMoneyTransferMT.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				MoneyTransferDialog dialog = new MoneyTransferDialog(
						getCardNumber(),
						editTransferCardNumberMT.getText().toString(),
						editTransferSumMT.getText().toString(),
						transferCurrency
				);
				dialog.show(getFragmentManager(), "moneyTransfer");
			}
		});

		return view;
	}


	public CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if (isChecked){
				switch (buttonView.getId()){
					case R.id.cbUan:
						cbUan.setChecked(true);
						cbUsd.setChecked(false);
						cbEur.setChecked(false);
						cbRus.setChecked(false);
						transferCurrency = "UAH";
						Log.d(LOG_TAG, "uan");
						break;
					case R.id.cbUsd:
						cbUan.setChecked(false);
						cbUsd.setChecked(true);
						cbEur.setChecked(false);
						cbRus.setChecked(false);
						transferCurrency = "USD";
						Log.d(LOG_TAG, "usd");
						break;
					case R.id.cbEur:
						cbUan.setChecked(false);
						cbUsd.setChecked(false);
						cbEur.setChecked(true);
						cbRus.setChecked(false);
						transferCurrency = "EUR";
						Log.d(LOG_TAG, "eur");
						break;
					case R.id.cbRus:
						cbUan.setChecked(false);
						cbUsd.setChecked(false);
						cbEur.setChecked(false);
						cbRus.setChecked(true);
						transferCurrency = "RUS";
						Log.d(LOG_TAG, "rus");
						break;
				}
			}
		}
	};


	public String getCardNumber(){
		//по id Card возвратим номер карты
		List<Card> cards = DBWork.getCardsFromDB();
		for (Card card : cards){
			if ( (AtributesBean.getInstance().getIdCard()) == card.getIdCard()){
				Log.d(LOG_TAG, "cardNumber(last) dig = " + card.getNumCard());
				return card.getNumCard();
			}
		}
		return "error";
	}


	private void getEditFields(){
		// TODO - нужно объязательно сделать проверки на допустимось вводимых значений
	}


}
